package database;

import java.util.ArrayList;

import baseclasses.Course;
import baseclasses.Grade;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

public class GradeDAO {

	//add grade to database returns -1 if error
	public static long addGrade(Context context, Grade grade){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.GRADENAME, grade.getName());
		values.put(DatabaseHelper.COURSEID, grade.getCourseId());
		values.put(DatabaseHelper.POINTSEARNED, grade.getPointsEarned());
		values.put(DatabaseHelper.POINTSOUTOF, grade.getPointsOutof());
		values.put(DatabaseHelper.ASSIGNMENTID, grade.getAssignment_id());

		long result = db.insert(DatabaseHelper.GRADETABLE, null, values);
		db.close();
		return result;

	}
	
	//return count of grades
	public static int gradesCount(Context context){
		
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		
		String sql = "SELECT COUNT(*) FROM " + DatabaseHelper.GRADETABLE;
	    SQLiteStatement statement = db.compileStatement(sql); //can only return a 1x1 result
	    int count = (int) statement.simpleQueryForLong(); //guarantees a simple 1x1 count result
		db.close();
	    return count;
	}

	//returns array list of grades given a course id
	public static ArrayList<Grade> getGradesByCourseId(Context context, int courseId){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		ArrayList<Grade> grades = new ArrayList<Grade>();
		Grade grade;

		Cursor cursor = db.query(DatabaseHelper.GRADETABLE, null, DatabaseHelper.COURSEID+" = "+courseId, null,null,null,null);
		//copy row data for each grade and return arraylist of copies
		while(cursor.moveToNext()){
			grade = new Grade(cursor.getInt(0),
					cursor.getInt(5),
					cursor.getString(1),
					cursor.getInt(2),
					cursor.getDouble(3),
					cursor.getDouble(4));

			grades.add(grade);
		}
		cursor.close();
		db.close();

		return grades;

	}
	
	//returns grade by assignment id
	public static Grade getGradeByAssignmentId(Context context, int assignment_id){
		
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		Grade grade = null;
		
		Cursor cursor = db.query(DatabaseHelper.GRADETABLE, null, DatabaseHelper.ASSIGNMENTID+" = "+assignment_id, null,null,null,null);
		
		if(cursor.moveToNext()){
			grade = new Grade(cursor.getInt(0),
					cursor.getInt(5),
					cursor.getString(1),
					cursor.getInt(2),
					cursor.getDouble(3),
					cursor.getDouble(4));
		}
		cursor.close();
		db.close();
		return grade;
		
	}

	//deletes all grades with specific course id, returns -1 if error
	public static int deleteGradesByCourse(Context context, Course course){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		int result = db.delete(DatabaseHelper.GRADETABLE, DatabaseHelper.COURSEID+" = "+course.getId(), null);

		db.close();

		return result;
	}

	//deletes grade from the database returns 0 if error
	public static int deleteGrade(Context context, Grade grade){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		int result = db.delete(DatabaseHelper.GRADETABLE, "_id = "+grade.getId(), null);

		db.close();

		return result;

	}

	//returns grade by id
	public static Grade getGradeById(Context context, int id){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.GRADETABLE, null, 
				"_id = "+id, null, null, null, null);

		cursor.moveToNext();
		Grade grade = new Grade(cursor.getInt(0),
				cursor.getString(1),
				cursor.getInt(2),
				cursor.getDouble(3),
				cursor.getDouble(4));
		
		cursor.close();
		db.close();


		return grade;
	}
	
	public static int editGrade(Context context, Grade grade){
		
		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.GRADENAME, grade.getName());
		values.put(DatabaseHelper.COURSEID, grade.getCourseId());
		values.put(DatabaseHelper.POINTSEARNED, grade.getPointsEarned());
		values.put(DatabaseHelper.POINTSOUTOF, grade.getPointsOutof());
		
		int result = db.update(DatabaseHelper.GRADETABLE, values, "_id = "+grade.getId(), null);
		
		db.close();
		
		return result;
		
	}


}
